package cn.edu.sdjzu.xg2121.controller;

import cn.edu.sdjzu.xg2121.domain.Department;
import cn.edu.sdjzu.xg2121.exception.BysjException;
import cn.edu.sdjzu.xg2121.service.DepartmentService;
import cn.edu.sdjzu.xg2121.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/departmentDeleteCtl")
public class DepartmentDeleteCtl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message_jsonObj = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(request);
        Department department = JSON.parseObject(req_jsonStr,Department.class);
        int id_int = department.getId();
        try {
            DepartmentService.getInstance().delete(id_int);
            response.getWriter().println("删除成功");
        }catch (BysjException e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
}